package com.smedi.ismedi.personnel.core.domain.service.impl;

import com.smedi.ismedi.personnel.core.domain.aggregatemodel.Assignment;
import com.smedi.ismedi.personnel.core.domain.service.AssignmentDomainService;
import com.smedi.ismedi.personnel.core.infrastructure.constant.CustomError;
import com.smedi.ismedi.personnel.core.domain.repository.AssignmentRepository;
import com.smedi.ismedi.personnel.core.port.adapter.service.OrganizationRemoteApiService;
import com.smedi.ismedi.web.starter.exception.LogicException;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

/**
 * @Author: xulinjun
 * @Date: 2021-6-8 15:40
 */
@Slf4j
@Service
@AllArgsConstructor
public class AssignmentDomainServiceImpl implements AssignmentDomainService {
    private final AssignmentRepository assignmentRepository;

    private final OrganizationRemoteApiService organizationRemoteApiService;

    @Override
    public Assignment saveAssignment(Assignment assignment) {
        Assignment basic;
        if (assignment.getAssignmentId() != null) {
            basic = assignmentRepository.selectByAssignmentId(assignment.getAssignmentId());
            if (basic == null) {
                throw LogicException.raise(CustomError.ASSIGNMENT_NOT_EXIST);
            }
        } else {
            basic = new Assignment();
        }

        basic.setPersonId(assignment.getPersonId())
                .setHrJob(assignment.getHrJob())
                .setHrOrganizationId(assignment.getHrOrganizationId());

//        // 判断当前组织的有效性
//        if (!organizationRemoteApiService.checkHrOrganizationAvailable(assignment.getHrOrganizationId())) {
//            throw LogicException.raise(CustomError.ORG_NOT_AVAILABLE);
//        }

        // 保存数据
        Assignment domain = (assignment.getAssignmentId() == null) ? assignmentRepository.doInsert(basic) : assignmentRepository.doUpdate(basic);
        return domain;
    }

    @Override
    public Assignment saveProjectOrganizationAssignment(Assignment assignment) {
        Assignment basic;
        if (assignment.getAssignmentId() != null) {
            basic = assignmentRepository.selectByAssignmentId(assignment.getAssignmentId());
            if (basic == null) {
                throw LogicException.raise(CustomError.ASSIGNMENT_NOT_EXIST);
            }
        } else {
            basic = new Assignment();
        }

        basic.setPersonId(assignment.getPersonId())
                .setHrJob(assignment.getHrJob())
                .setHrOrganizationId(assignment.getHrOrganizationId());

        // 判断当前组织的有效性
        if (!organizationRemoteApiService.checkHrOrganizationAvailable(assignment.getHrOrganizationId())) {
            throw LogicException.raise(CustomError.ORG_NOT_AVAILABLE);
        }

        // 保存数据
        Assignment domain = (assignment.getAssignmentId() == null) ? assignmentRepository.doInsert(basic) : assignmentRepository.doUpdate(basic);
        return domain;
    }

    @Override
    public Boolean checkHrOrganizationHasAssignments(Long hrOrganizationId) {
        return assignmentRepository.selectAssignmentCountByHrOrganizationId(hrOrganizationId) > 0;
    }

    @Override
    public void deleteInternalAssignmentsByPersonId(Long personId) {
        assignmentRepository.doDeleteByPersonId(personId);
    }
}
